package poketrade;

import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import poketrade.entities.User;
import poketrade.managers.UserManager;

@WebServlet
public class AuthServlet extends BaseHttpServlet {

	private static final long serialVersionUID = 1L;

	@EJB
	private UserManager mgr;
	
	@Override
	public void init()
    {
		// Sample user Moi
		User u = new User();
		u.setPseudo("Moi");
		u.setNom("Pefonvrille");
		u.setPrenom("Omendadrir");
		u.setSha1Password("pipo");
		u.setAdresse("7 rue saint denis Toulouse");
		u.setCote(100);
		u.setDepartement("31");
		u.setEmail("foobar@example.com");
		u.setSolde(42.0f);
		u.setVille("Toulouse");
		u.setAdmin(true);
		u.setPremium(true);
		mgr.subscribe(u);
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		if (action != null && action.equals("signout"))
			doHandleSignout(request, response);
		else
			forward(request, response, "NewsServlet");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		if (action != null)
		{
			if (action.equals("login"))
				doHandleLogin(request, response);
			else if (action.equals("signup"))
				doHandleSignup(request, response);
		}
		else
			forward(request, response, "NewsServlet");
	}

	private void doHandleSignup(HttpServletRequest request, HttpServletResponse response) {
		User u = new User();
		// TODO Check user input
		u.setPseudo(request.getParameter("pseudo"));
		u.setNom(request.getParameter("nom"));
		u.setPrenom(request.getParameter("prenom"));
		u.setEmail(request.getParameter("email"));
		u.setAdresse(request.getParameter("adresse"));
		u.setDepartement(request.getParameter("departement"));
		u.setVille(request.getParameter("ville"));
		u.setSha1Password(request.getParameter("password"));
		// TODO Date de naissance
		u.setAdmin(false);
		u.setPremium(false);
		mgr.subscribe(u);
		// TODO Mail verification
		
		HttpSession s = request.getSession(true);
		s.setAttribute("user", u);
		showMessage(request, response, "Account created !");
	}
	
	private void doHandleLogin(HttpServletRequest request, HttpServletResponse response) {
		User u = mgr.handleLogin(request.getParameter("login"), request.getParameter("password"));
		if (u != null)
		{
			HttpSession s = request.getSession(true);
			s.setAttribute("user", u);
			showMessage(request, response, "Login successful !");
		}
		else
		{
			// If there's a current session, invalidate it.
			HttpSession s = request.getSession(false);
			if (s != null)
				s.invalidate();
			
			showMessage(request, response, "Login failed");
		}
	}
	
	private void doHandleSignout(HttpServletRequest request, HttpServletResponse response) {
		HttpSession s = request.getSession(false);
		if (s != null)
			s.invalidate();
		request.setAttribute("user", null);
		showMessage(request, response, "Disconnected ;)");
	}
}
